package com.sxfq.loansupermarket.backsystem.modular.logs.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.logs.entity.ReservationsDeleteLogInfo;
import com.sxfq.loansupermarket.backsystem.modular.logs.entity.ReservationsUpdateLogInfo;
import com.sxfq.loansupermarket.backsystem.modular.logs.entity.SearchReservationsDeleteLogInfo;
import com.sxfq.loansupermarket.backsystem.modular.logs.entity.SearchReservationsUpdateLogInfo;
import com.sxfq.loansupermarket.backsystem.modular.logs.service.IReservationsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 预约客户信息
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/reservationsLog")
public class ReservationsLogController extends BaseController {

    @Autowired
    IReservationsLogService service;

    private String PREFIX = "/logs/reservationsLog/";


    @RequestMapping(value = "/delete")
    public String delete(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "/reservations_delete_log.html";
    }

    @RequestMapping(value = "/update")
    public String update(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "/reservations_update_log.html";
    }

    /**
     *
     */
    @RequestMapping("/deleteList")
    @ResponseBody
    public Object deleteList(SearchReservationsDeleteLogInfo info, HttpServletRequest request) {
        Page<ReservationsDeleteLogInfo> page = new PageFactory<ReservationsDeleteLogInfo>().defaultPage();
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        try {
            List<ReservationsDeleteLogInfo> list = service.getDeleteList(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    @RequestMapping("/updateList")
    @ResponseBody
    public Object updateList(SearchReservationsUpdateLogInfo info, HttpServletRequest request) {
        Page<ReservationsUpdateLogInfo> page = new PageFactory<ReservationsUpdateLogInfo>().defaultPage();
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        try {
            List<ReservationsUpdateLogInfo> list = service.getUpdateList(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }
}
